package com.ruoyi.project.mobile.customer;

import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.common.utils.R;
import com.ruoyi.common.utils.RFactory;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.project.system.customer.customer.domain.Customer;
import com.ruoyi.project.system.customer.customer.service.ICustomerService;
import com.ruoyi.project.system.customer.deal.domain.CustomerDeal;
import com.ruoyi.project.system.customer.deal.service.ICustomerDealService;
import com.ruoyi.project.system.house.house.enums.HouseDto;
import com.ruoyi.project.system.house.house.service.IHouseService;
import com.ruoyi.project.system.house.img.domain.HouseImg;
import com.ruoyi.project.system.house.img.service.IHouseImgService;

/**
 * 功能：
 *
 * @author：F_baisi
 * @create：2020/8/30 13:01
 * @version：2020 Version：1.0
 * @company：小牛科技 Created with IntelliJ IDEA
 */
@RestController
@RequestMapping("/api/customer")
public class ApiCustomerController extends BaseController {

	@Autowired
	private ICustomerService customerService;

	@Autowired
	private IHouseService houseService;

	@Autowired
	private IHouseImgService houseImgService;

	@Autowired
	private ICustomerDealService customerDealService;

	/**
	 * 查询客户列表
	 * @param uid
	 * @return
	 */
	@GetMapping
	public R customerList(@RequestParam String uid,
						  @RequestParam Integer status,
						  @RequestParam(required = false) String name){
		startPage();
		Customer select = new Customer();
		select.setMemberId(uid);
		select.setStatus(status);
		select.setName(name);
		List<Customer> customers = this.customerService.selectCustomerList(select);
		for(Customer customer :customers){
			if(customer.getHouseId()!=null){
				HouseDto houseDto = this.houseService.selectHouseById(customer.getHouseId());
				//设置图片
				HouseImg houseImg = new HouseImg();
				houseImg.setHouseId(houseDto.getId());
				List<HouseImg> houseImgs = this.houseImgService.selectHouseImgList(houseImg);
				houseDto.setHouseImgs(houseImgs.isEmpty()?null: java.util.Arrays.asList(houseImgs.get(0)));
				customer.setHouse(houseDto);
			}
		}
		return RFactory.generateR(getDataTable(customers));
	}

	/**
	 * 查询客户详情
	 * @param uid
	 * @param id
	 * @return
	 */
	@GetMapping("/info")
	public R customerInfo(@RequestParam String uid,@RequestParam String id){
		HashMap<String, Object> map = new HashMap<>();
		//客户信息
		Customer customer = this.customerService.selectCustomerById(id);
		//房子信息
		if(customer.getHouseId()!=null){
			HouseDto houseDto = this.houseService.selectHouseById(customer.getHouseId());
			//设置图片
			HouseImg houseImg = new HouseImg();
			houseImg.setHouseId(houseDto.getId());
			List<HouseImg> houseImgs = this.houseImgService.selectHouseImgList(houseImg);
			houseDto.setHouseImgs(houseImgs.isEmpty()?null: java.util.Arrays.asList(houseImgs.get(0)));
			customer.setHouse(houseDto);
		}
		map.put("customer",customer);
		//交易信息
		CustomerDeal customerDeal = new CustomerDeal();
		customerDeal.setCustomerId(id);
		List<CustomerDeal> customerDeals = this.customerDealService.selectCustomerDealList(customerDeal);
		map.put("customerDeals",customerDeals);
		return RFactory.generateR(map);
	}


}
